{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c1e376e6",
   "metadata": {},
   "source": [
    "# 上周回顧\n",
    "\n",
    "> 1. while循环：while ture ---无限循环\n",
    ">> 1. 指定次数，while ture 可以实现for循环的指定次数，增加一个count计数器即可\n",
    ">> 2. while ture 一般用户输入一起使用，让客户可以重复输入，知道某些条件成立结束输入（购物车）\n",
    "> 2. 购物车实践\n",
    "\n",
    "\n",
    "# 本周内容\n",
    "> 1. 元组\n",
    "> 2. 集合\n",
    "> 3. 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3c452b9",
   "metadata": {},
   "source": [
    "## 1.元组（tuple）\n",
    "> 1. 元组的基本内容（形态，定义）\n",
    "> 2. 元组的性质\n",
    ">> 1. 有序性（indexing，sliceing）\n",
    ">> 2. <font color=\"red\">**不可变性**</font> 元组没有增，删，改\n",
    "\n",
    "\n",
    "> 3. 元组的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "994f8daa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2110130078, 180001, 180002) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "kenny_学号 = (2110130078,180001,180002)\n",
    "print(kenny_学号,type(kenny_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "00be68a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2110130078, 180001, 180002) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "kenny_学号 = 2110130078,180001,180002\n",
    "print(kenny_学号,type(kenny_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2a05d8b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2110130078"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kenny_学号[0] # 展示了元组的有序性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7bdbedfc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(180001, 180002)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kenny_学号[1:]  # 有序性的特别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8aeebfd1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=25, tm_hour=15, tm_min=6, tm_sec=39, tm_wday=1, tm_yday=298, tm_isdst=0)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组的实际案例\n",
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f9afbb5b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on struct_time object:\n",
      "\n",
      "class struct_time(builtins.tuple)\n",
      " |  struct_time(iterable=(), /)\n",
      " |  \n",
      " |  The time value as returned by gmtime(), localtime(), and strptime(), and\n",
      " |  accepted by asctime(), mktime() and strftime().  May be considered as a\n",
      " |  sequence of 9 integers.\n",
      " |  \n",
      " |  Note that several fields' values are not the same as those defined by\n",
      " |  the C language standard for struct tm.  For example, the value of the\n",
      " |  field tm_year is the actual year, not year - 1900.  See individual\n",
      " |  fields' descriptions for details.\n",
      " |  \n",
      " |  Method resolution order:\n",
      " |      struct_time\n",
      " |      builtins.tuple\n",
      " |      builtins.object\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __reduce__(...)\n",
      " |      Helper for pickle.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  tm_gmtoff\n",
      " |      offset from UTC in seconds\n",
      " |  \n",
      " |  tm_hour\n",
      " |      hours, range [0, 23]\n",
      " |  \n",
      " |  tm_isdst\n",
      " |      1 if summer time is in effect, 0 if not, and -1 if unknown\n",
      " |  \n",
      " |  tm_mday\n",
      " |      day of month, range [1, 31]\n",
      " |  \n",
      " |  tm_min\n",
      " |      minutes, range [0, 59]\n",
      " |  \n",
      " |  tm_mon\n",
      " |      month of year, range [1, 12]\n",
      " |  \n",
      " |  tm_sec\n",
      " |      seconds, range [0, 61])\n",
      " |  \n",
      " |  tm_wday\n",
      " |      day of week, range [0, 6], Monday is 0\n",
      " |  \n",
      " |  tm_yday\n",
      " |      day of year, range [1, 366]\n",
      " |  \n",
      " |  tm_year\n",
      " |      year, for example, 1993\n",
      " |  \n",
      " |  tm_zone\n",
      " |      abbreviation of timezone name\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  n_fields = 11\n",
      " |  \n",
      " |  n_sequence_fields = 9\n",
      " |  \n",
      " |  n_unnamed_fields = 0\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __add__(self, value, /)\n",
      " |      Return self+value.\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      Return key in self.\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(self, key, /)\n",
      " |      Return self[key].\n",
      " |  \n",
      " |  __getnewargs__(self, /)\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __hash__(self, /)\n",
      " |      Return hash(self).\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __mul__(self, value, /)\n",
      " |      Return self*value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __rmul__(self, value, /)\n",
      " |      Return value*self.\n",
      " |  \n",
      " |  count(self, value, /)\n",
      " |      Return number of occurrences of value.\n",
      " |  \n",
      " |  index(self, value, start=0, stop=9223372036854775807, /)\n",
      " |      Return first index of value.\n",
      " |      \n",
      " |      Raises ValueError if the value is not present.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __class_getitem__(...) from builtins.type\n",
      " |      See PEP 585\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(time.localtime())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "273b8185",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4], [3, 4, 5, 6]) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "tuple_text = ([1,2,3,4],[3,4,5,6])\n",
    "print(tuple_text,type(tuple_text))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9b45a0db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 100, 100, 100, 100] <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "tuple_text[0].append(100)\n",
    "print(tuple_text[0],type(tuple_text))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2ebb74ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一般情况下，元组里面存放的是 常量（不可变）， 常量：数字，字符串\n",
    "#                                       不存放变量 如：列表，字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "050beeb4",
   "metadata": {},
   "source": [
    "## 2.集合（sets）\n",
    "> 1. 集合的基本概念\n",
    ">> 1. 性质：没有重复项（去重）\n",
    ">> 2. 无序性\n",
    "> 2. 集合的一般方法\n",
    ">> 1. 集合的运算：& ，\n",
    ">> 2. 集合的一般方法：intersection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "eea5ccce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'oringe', 'apple', 'pear']\n",
      "{'oringe', 'pear', 'apple'}\n"
     ]
    }
   ],
   "source": [
    "basket_lists = ['apple','oringe','apple','pear']\n",
    "basket_sets = {'apple','oringe','apple','pear'} ## 集合能取出重复项\n",
    "print(basket_lists)\n",
    "print(basket_sets )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1f53f784",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'set' object is not subscriptable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [19]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mbasket_sets\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'set' object is not subscriptable"
     ]
    }
   ],
   "source": [
    "basket_sets[0]  ## 无序性"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "374a3d31",
   "metadata": {},
   "source": [
    "## 集合的交集 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "cb166f66",
   "metadata": {},
   "outputs": [],
   "source": [
    "公司1 = {1,2,3,4,5,6}\n",
    "公司2 = {7,8,9,10,1,2}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9e7c3dfd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "公司1 & 公司2 ##集合的交集 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "01aa8972",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "公司1.intersection(公司2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf12a272",
   "metadata": {},
   "source": [
    "## 集合的或运算，并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ed84fbcf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "公司1 | 公司2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "c3b19551",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(公司1 | 公司2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "a6f682b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "公司1.union(公司2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a11170b",
   "metadata": {},
   "source": [
    "# 3. <font color=\"red\">字典(dict)</font>\n",
    "\n",
    "> 1. 字典的基本概念：\n",
    ">> 1. 字典性质：\n",
    ">>> 1. 无序性\n",
    ">>> 2. keys 是不可变的:就像列表，字符串等有序的数据结构的index不可变一样的\n",
    ">>> 3.  keys 是唯一的\n",
    ">> 2. 字典的形态：\n",
    "> 2. 字典的一般方法\n",
    ">> 1. 字典的创建\n",
    ">> 2. 字典的函数和方法\n",
    ">>> 1. 增加\n",
    ">>> 2. 删除\n",
    ">>> 3. 修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "29fc9136",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 字典形态，举例：\n",
    "公司3 = {\n",
    "    121:\"kenny\",\n",
    "    122:\"bill\",\n",
    "    123:\"jack\",\n",
    "    124:\"marry\"\n",
    "} ## 序号是唯一的，作为字典的key "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "5f2fc8c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{121: 'kenny', 122: 'bill', 123: 'jack', 124: 'marry'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(公司3,type(公司3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "83bbf547",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{121: 'kenny', 122: 'bill', 123: 'jack', 124: 'marry', 125: 'kenny'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "公司4 = {\n",
    "    121:\"kenny\",\n",
    "    122:\"bill\",\n",
    "    123:\"jack\",\n",
    "    124:\"marry\",\n",
    "    125:\"kenny\"\n",
    "}## key 是唯一的，结果可以重复\n",
    "print(公司4,type(公司4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e873ea2e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'kenny'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 字典的取值：通过key 进行取值\n",
    "公司3[121]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "520e01e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "公司3 = {\n",
    "    121:[\"kenny\",\"广州\",\"man\",19,\"聊天记录\"],\n",
    "    122:\"bill\",\n",
    "    123:\"jack\",\n",
    "    124:\"marry\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "47d7c464",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['kenny', '广州', 'man', 19, '聊天记录']"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "公司3[121]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a02a0ff",
   "metadata": {},
   "source": [
    "## 3.2 字典的一般方法\n",
    "* 1. 按照字典的形式直接创建（key：value）\n",
    "\n",
    "公司3 = {\n",
    "    121:\"kenny\",\n",
    "    122:\"bill\",\n",
    "    123:\"jack\",\n",
    "    124:\"marry\"\n",
    "}\n",
    "* 2. 使用dict（）方法创建字典\n",
    "* 3. 使用字典增加的方式创建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "847c8f0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用dict（）方法创建字典\n",
    "公司5 = dict(\n",
    "    stu121=\"kenny\",\n",
    "    stu122=\"bill\",\n",
    "    stu123=\"marry\",\n",
    "    stu124=\"jack\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b52f445c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu121': 'kenny', 'stu122': 'bill', 'stu123': 'marry', 'stu124': 'jack'}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "公司5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "6dc1dd05",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用字典增加的方式创建字典\n",
    "\n",
    "网一 = {}\n",
    "网一[\"stu121\"] = 'kenny'\n",
    "网一[\"stu122\"] = 'marry'\n",
    "网一[\"stu123\"] = 'jack'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "356f483a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu121': 'kenny', 'stu122': 'marry', 'stu123': 'jack'}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "a9cc232b",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_keys = [\"stu121\",\"stu122\",\"stu123\"]\n",
    "网一_values = ['kenny','marry','jack']\n",
    "网一 = {}\n",
    "for i in range(3):\n",
    "    网一[网一_keys[i]] = 网一_values[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "20d92d7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu121': 'kenny', 'stu122': 'marry', 'stu123': 'jack'}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c726877d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
